﻿using System;
using System.Collections.Generic;
using BrnMall.Manager.Domain.WeApp;

namespace BrnMall.Manager.Manager.WeApp
{
    public static class TemplateMessageMananger
    {
        public static void BatchInsert(IList<TemplateMessage> messages)
        {
            using (var db = new QDMallDatabase())
            {
                try
                {
                    db.BeginTransaction();
                    foreach (var message in messages)
                    {
                        db.Insert(message);
                    }
                    db.CompleteTransaction();
                }
                catch (Exception)
                {

                    db.AbortTransaction();
                }

            }
        }
        public static void Insert(TemplateMessage message)
        {
            using (var db = new QDMallDatabase())
            {
                        db.Insert(message);
            }
        }
        public static void BatchDelete(IList<TemplateMessage> messages)
        {
            using (var db = new QDMallDatabase())
            {
                db.BeginTransaction();
                foreach (var message in messages)
                {
                    db.Delete<TemplateMessage>(message.MsgId);
                }
                db.CompleteTransaction();
            }
        }

        public static void Delete(long msgId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<TemplateMessage>(msgId);
            }
        }

        public static IList<TemplateMessage> FetchList(int count)
        {
            using (var db = new QDMallDatabase())
            {

                return db.Fetch<TemplateMessage>($"select top {count} * from wechat_templatemessage order by msgid desc");

            }
        }
    }
}
